package com.open.capacity.reptile.task;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.open.capacity.finance.task.ScheduledOfTask;
import com.open.capacity.reptile.constant.PurchaseNoticeConstant;
import com.open.capacity.reptile.dto.purchase.NoticeResponse;
import com.open.capacity.reptile.service.SourcePurchaseNoticeService;
import com.open.capacity.reptile.util.HttpUtils;
import com.open.capacity.reptile.util.VerifyCodeUtils;
import com.open.capacity.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Component
@Slf4j
public class ReptileOrgDataTask implements ScheduledOfTask {

    @Autowired
    private SourcePurchaseNoticeService service;

    @Autowired
    private TaskExecutor taskExecutor;


    @Value("${file.uploadpath:/usr/local/files/upload}")
    private String fileuploadPath;

    Integer cronId = null;

    @Override
    public void setCronId(Integer cronId) {
        cronId = cronId;
    }

    @Override
    public void execute() throws UnsupportedEncodingException {
//        String lastDate = service.findLastDate();
//        String operationStartTime = "";
//        if (StringUtil.isNotEmpty(operationStartTime)) {
//            operationStartTime = lastDate.substring(0, 10) + " 00:00:00";
//        }

        String verifyCode = VerifyCodeUtils.getVerifyCode(PurchaseNoticeConstant.verifyCodeUrl, fileuploadPath);
        String url = MessageFormat.format(PurchaseNoticeConstant.requestUrl, 1, 10, verifyCode, URLEncoder.encode(PurchaseNoticeConstant.KEYWORD,"UTF-8"), "");
        String html = HttpUtils.requestGet(url);
        List<Callable<Object>> callableList = new ArrayList<>();
        NoticeResponse t = JSONObject.parseObject(html, new TypeReference<NoticeResponse>() {
        });
        if (t != null) {
            log.info(Thread.currentThread().getName() + "：总记录数--" + t.getTotal());
//            service.save(t.getData());
            if (t.getCode().equals("200")) {
                int pageCount = t.getTotal() / PurchaseNoticeConstant.PAGE_SIZE;
                for (int i = 1; i <= pageCount + 1; i++) {
                    Callable callable = new SyncPurchaseNoticeRunable(i, fileuploadPath, verifyCode, service);
                    callableList.add(callable);
                }
            }
        }

        ExecutorService executorService = Executors.newFixedThreadPool(8);
        try {
            executorService.invokeAll(callableList);
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }
    }

    @Override
    public void trigger(String startTime) throws IOException {

    }
}

